package com.healthcloud.service.impl;

import java.util.ArrayList;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.healthcloud.dao.InsuranceMapper;
import com.healthcloud.entity.Insurance;
import com.healthcloud.service.InsuranceService;

/**
 * Created by XYC on 2015/7/17.
 */
@Service
public class InsuranceServiceImpl implements InsuranceService {
    @Autowired
    private InsuranceMapper insuranceMapper;

    @Override
    public ArrayList<Insurance> findByIdCard(Insurance insurance) {
        Insurance insurance1=new Insurance();
        insurance1.setIdCard(insurance.getIdCard());
        return findByEntity(insurance1);
    }

    @Override
    public ArrayList<Insurance> findByEntity(Insurance insurance) {
        return insuranceMapper.findByEntityWithOutPage(insurance);
    }

    @Override
    public Insurance saveOrUpdata(Insurance insurance) {
        ArrayList<Insurance> insurances = findByIdCard(insurance);
        if(insurances.isEmpty()){
            insurance.setUpdatedTime(new Date());
            insurance.setCreatedTime(new Date());
            insuranceMapper.save(insurance);
        }else{
            insurance.setId(insurances.get(0).getId());
            insurance.setCreatedTime(insurances.get(0).getCreatedTime());
            insurance.setUpdatedTime(new Date());
            insuranceMapper.update(insurance);
        }
        return insurance;
    }
}
